<template>
	<view class="withdraw-container">
		<u-popup :show="selectbankstatus" overlayOpacity="0.8" bgColor="transparent" mode="bottom" @close="closeselect">
			<view class="bank-list-block">
				<!-- <view class="bank-close-icon" @tap="selectbankstatus = false"></view> -->
				<view class="bank-title">
					<view class="cancel" @tap="selectbankstatus = false">取消</view>
					<view class="sure" @tap.stop="sureBank">确定</view>
				</view>
				<view class="bank-list-msg-block">
					<view class="bank-list-msg" v-for="(item,index) in bankList"
						:style="{Background:chooseBank.id==item.id?'#F5F5F5':''}" :key="index" @tap.stop="choosebank(item,index)">
						<view class="bank-list-msg-arr">
							<view class="bank-msg-text">
								<view class="bank-icon"></view>
								<view class="bank-name">
									{{item.account_name}}({{item.account | conceal(4)}})
								</view>
							</view>
							<view class="bank-choose-active" v-if="currIndex == index"></view>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
		<!-- 头部 -->
		<pagehead pagesPath="/pages/asset/asset">
			提现
			<!-- #ifdef	H5 || APP-PLUS -->
			<view class="header-right" @tap.stop="$tools.jump('/pages/asset/withdrawlog',cashType)">提现记录</view>
			<!-- #endif -->
		</pagehead>
		<view class="form-box">
			<view class="form-item border-b">
				<view class="form-item-label">
					提现至：
				</view>
				<view class="form-right" @tap.stop="selectbank" >
					<text v-if="form.account">{{form.account | conceal(4)  }}</text> 
					<text v-else>请选择</text>
					<image class="ico" :src="IMAGE_STATIC+'/images/arrow-right.png'" mode="scaleToFill"/>
				</view>
			</view>
			<view class="form-item">
				<view class="form-item-label">
					账户余额：
				</view>
				<view class="form-right">
					{{ balanceObj.validBalance }}
				</view>
			</view>
		</view>
		<view class="form-box">
			<view class="form-item border-b">
				<view class="unit">
					￥
				</view>
				<input type="number" class="form-item-inner price" v-model="form.withdrawnum">
				<view class="form-right" @click.native="maxnum()">
					<text class="red">全部</text>
				</view>
			</view>
				<view class="form-item">
					<view class="form-item-label">
						手续费：
					</view>
					<view class="form-right">
						{{ feeNum }}
					</view>
				</view>
				<view class="form-item">
					<view class="form-item-label">
						到账金额：
					</view>
					<view class="form-right">
						{{ receivedNum }}
					</view>
				</view>
				<view class="form-item">
					<view class="form-item-label" style="width:200rpx">
						交易密码：
					</view>
					<view class="form-right">
						<input type="password" class="form-item-inner" style="" placeholder="请输入交易密码" v-model="form.password">
					</view>
				</view>
			<view class="warn">
				<view class="p"><text class="dot"></text>提现手续费每笔{{ Number(this.cashObj.cashSingleCahrgeRate)*100}}%+3元</view>
				<!-- <view class="p"><text class="dot"></text>单笔提现金额10.00~20000.00元</view>
				<view class="p"><text class="dot"></text>单日最大笔数5笔，当日最大金额50000.00元</view> -->
				<view class="p"><text class="dot"></text>提现时间: {{ cashObj.cashOpenStartTime }}~{{ cashObj.cashOpenEndTime }}</view>
				<view class="p"><text class="dot"></text> 提现资金结算到申请人绑定的结算卡</view>
				<view class="p"><text class="dot"></text>如果遇到系统繁忙等情况延迟到账,详情请留意系统通知</view>
			</view>
		</view>
		<view class="form-item" @tap.stop="getwithdraw">
			<view class="submit">
				确定
			</view>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<view class="text-center" @tap.stop="$tools.jump('/pages/asset/withdrawlog',cashType)">
		<text>提现记录</text></view>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				IMAGE_STATIC: this.$url.assetsPath, // 静态图片地址
				form:{
					withdrawnum: '',
					cardNo:'',
					bankId:''
				},
				chooseBank:{bankId:''},
				selectbankstatus: false,
				currIndex:-1,
				bankList: [
					{bankId:1,bankName:'建设银行',cardNo:'658545457487875454'}
				],
				balanceObj: {
					validBalance:0
				},
				cashObj:{cashOpenStartTime:'',cashOpenEndTime:''}, //提现信息
				cashType:''
			}
		},
		computed:{
			feeNum(){
				let sum =0
				if(this.form.withdrawnum!=0){
					sum = (Number(this.form.withdrawnum) * Number(this.cashObj.cashSingleCahrgeRate)).toFixed(2) 
					if(sum < Number(this.cashObj.cashMinCahrgeNum)){
					sum = this.cashObj.cashMinCahrgeNum
					}
				}
				return Number(sum) + 3
			},
			receivedNum(){
				let num =0
				if(this.form.withdrawnum!=0){
					num = Number(this.form.withdrawnum)  - Number(this.feeNum)
				}
				num = num>0?num : 0;
				return num
			}
		},
		onLoad(options) {
			if(options){
				this.cashType = options.value1
			}
		},
		onShow() {
			this.init();
		},
		methods: {
			init(){
				this.getCashPageInfo();
				this.getBankList();
			},
			// 提现
			getwithdraw(){
				let that = this;
				if(!that.form.withdrawnum || that.form.withdrawnum <=0){
					return that.$tools.toast('请输入提现金额');
				}
				if(that.balanceObj.validBalance ==0 || that.balanceObj.validBalance < that.form.withdrawnum){
					return that.$tools.toast('可提现金额不足');
				}
				if(that.form.withdrawnum < that.cashObj.minCashMoney){
					return that.$tools.toast('提现金额不能低于'+that.cashObj.minCashMoney);
				}
				if(that.bankList.length == 0){
					return that.$tools.toast('请添加银行卡');
				}
				if(!that.form.bankId){
					return that.$tools.toast('请选择提现银行卡');
				}
				if(!that.form.password){
					return that.$tools.toast('请输入交易密码');
				}
				let data = {
					pay_password: that.$md5(that.form.password),
					account_id: that.form.bankId,
					cash_type: that.cashType,
					cash_money: that.form.withdrawnum,
				}
				console.log('data',data)
				that.$Ajax('/api/wallet/cash/userApplyCash', data, res => {
					if(res.success){
						that.$tools.toastJump('提交成功','/pages/asset/withdrawlog',this.cashType)
						that.withdrawnum = '';
					}else{
						console.log('code',res.code)
						if(res.code=='code_999177'){
							that.$tools.toast(res.msg)
							setTimeout(()=>{
								that.goSing(res.data)
							},1000)
						}
						that.$tools.toast(res.msg);
					}
				},err => {
					console.log('code',err)
					that.$tools.toast(err.msg)
					setTimeout(()=>{
						that.goSing(err.data)
					},1000)
					// that.$tools.toast(err);
				})
			},
			// 去签约
			goSing(url){
				console.log('跳转')
				// #ifdef H5
				location.href = url
				// window.open(url)
				// #endif

				// #ifdef MP-WEIXIN
				uni.setStorageSync('singUrl',encodeURIComponent(url))
				this.$tools.jump('/pages/service/service','','','/pages/asset/withdrawlog?value1='+this.cashType)
				// #endif

				// #ifdef APP-PLUS
				this.$tools.jump('/pages/service/service',url,'','/pages/asset/withdrawlog?value1='+this.cashType)
				// #endif
			},
			// 提现信息
			getCashPageInfo() {
				const data={
					cash_type:this.cashType
				}
				console.log('data',data)
				this.$Ajax('/api/wallet/cash/getCashPageInfo',data,res => {
					if(res.success){
						const cashObj = res.data.userCashInfo
						this.$set(this,'cashObj',cashObj)
						this.balanceObj.validBalance = this.cashType=='01'?res.data.userInfo.purse_type_01:res.data.userInfo.purse_type_02
						console.log('cashObj',this.cashObj)
					}
				})
			},
			// 确定银行卡号
			sureBank(){
				if(!this.chooseBank.id){
					return this.$tools.toast('请选择提现银行卡');
				}
				this.form.account = this.chooseBank.account
				this.form.bankId = this.chooseBank.id
				this.selectbankstatus = false
			},
			getBankList(){
				let that = this;
				const data = {}
				that.$Ajax('/api/user/account/getUserAccountInfo', data, res => {
					if (res.success) {
						that.bankList = res.data.userAccountInfo;
						if(that.bankList.length<=0){
							return that.$tools.toastJump('请绑定银行卡','/pages/bank/bank');
						}
					}
				}, err => { })
			},
			getneedinput(e) {
				const inputRule = /^(0+)|[^\d]+/g //修改inputRule 的值
				// this.$nextTick(function() {
				// 	this.pledgenum = e.replace('-', '')
				// })
			},
			maxnum() {
				console.log('全部');
				this.form.withdrawnum = this.balanceObj.validBalance;
			},
			selectbank() {
				this.selectbankstatus = true
			},
			closeselect() {
				console.log(123);
				this.selectbankstatus = false
			},
			choosebank(item,index) {
				this.currIndex = index;
				this.chooseBank.account_name = item.account_name
				this.chooseBank.id = item.id
				this.chooseBank.account = item.account
			}

		}
	}
</script>

<style scoped lang="scss">
@import '@/common/scss/operate';
	.withdraw-container {
		width: 100%;
		height: 100vh;
		.form-box{
			padding: 36rpx 24rpx;
		}
		.form-right{
		}
		.price{
            text-align: left !important;
            color: #333333;
			font-size: 64rpx;
			width: calc(100% - 200rpx);
        }
        .uni-input-input,.uni-input-placeholder {
            text-align: left;
            // direction: rtl;
            // unicode-bidi: bidi-override;
        }
		.price /deep/ .uni-input-input{
			width:100%;
			text-align: left !important;
		}
		.unit{
			font-size: 48rpx;
		}
		.submit{
			height: 90rpx;
			line-height: 90rpx;
			background: #AD2926;
			border-radius: 118rpx;
			width: 92%;
			margin: 20rpx auto;
			text-align: center;
			color: #fff;
			font-size: 30rpx;
			margin-top: 60rpx;
		}
		.warn{
			margin-top: 48rpx;
			font-size: 24rpx;
			color: #666666;
			// line-height: 44rpx;
			.p{
				margin-top: 18rpx;
			}
			.dot{
				display: inline-block;
				width: 10rpx;
				height: 10rpx;
				background: #555;
				border-radius: 100%;
				margin-right: 10rpx;
				vertical-align: middle;
			}
		}
		.bank-list-block {
			width: 750rpx;
			height: 600rpx;
			background: #FFFFFF;
			position: relative;

			.bank-close-icon {
				width: 40rpx;
				height: 40rpx;
				background: url($uni-static-dir+'/userimage/bank-icon.png') 100% no-repeat;
				background-size: 100% 100%;
				position: absolute;
				top: 63rpx;
				left: 40rpx;
				z-index: 3;
			}

			.bank-title {
				width: 100%;
				padding: 0 24rpx;
				font-size: 24rpx;
				margin-top: 32rpx;
				box-sizing: border-box;
				margin-bottom: 20rpx;
				// border-bottom: 1rpx solid #E5E5E5;
				@include flexBetween;
				.sure{
					color: #AD2926;
				}
			}

			.bank-list-msg-block {
				width: 100%;
				.bank-list-msg {
					width: 100%;
					padding: 0 24rpx;
					.bank-list-msg-arr {
						// width: 95%;
						padding: 32rpx 0;
						display: flex;
						justify-content: space-between;
						align-items: center;
						align-self: flex-end;
						border-bottom: 1rpx solid #E5E5E5;

						.bank-msg-text {
							display: flex;
							align-items: center;

							.bank-icon {
								width: 52rpx;
								height: 32rpx;
								background: url($uni-static-dir+'/userimage/pay-icon.png') 100% no-repeat;
								background-repeat: no-repeat;
								background-size: 100% 100%;
							}

							.bank-name {
								font-size: 32rpx;
								font-family: PingFang SC-Regular, PingFang SC;
								font-weight: 400;
								color: #333333;
								margin-left: 30rpx;
							}
						}

						.bank-choose-active {
							width: 40rpx;
							height: 40rpx;
							background: url($uni-static-dir+'/userimage/bank-confrim.png') 100% no-repeat;
							background-size: 100% 100%;
							margin-right: 40rpx;
						}
					}


				}
			}
		}
		.text-center{
			margin-top: 62rpx;
			font-size: 28rpx;
			color: #333333;
			line-height: 48rpx;
			text-align: center;
			text{
				border-bottom: 1px solid #AEAEAE;
			}
		}
	}
</style>
